package com.edusite

class DocumentosController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
    if (session.user.tipo != "admin" && session.user.tipo != "profesor")
    	redirect(uri:"/")
    	
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [documentosInstanceList: Documentos.list(params), documentosInstanceTotal: Documentos.count()]
    }
        def listProfe = {
    if (session.user.tipo != "profesor")
    	redirect(uri:"/")
    	
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        def documentosList=Documentos.findAllByPertenece(session.user,params)
        def documentosListTotal=documentosList.size()
        [documentosInstanceList: documentosList, documentosInstanceTotal: documentosListTotal]
    }

    def create = {
    if (session.user.tipo != "admin")
    	redirect(uri:"/")
        def documentosInstance = new Documentos()
        documentosInstance.properties = params
        return [documentosInstance: documentosInstance]
    }
    def createProfe = {
    if (session.user.tipo != "profesor")
    	redirect(uri:"/home/indice")
        def documentosInstance = new Documentos()
        documentosInstance.properties = params
        return [documentosInstance: documentosInstance]
    }

    def save = {
        def documentosInstance = new Documentos(params)
        if (documentosInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'documentos.label', default: 'Documentos'), documentosInstance.id])}"
            redirect(action: "show", id: documentosInstance.id)
        }
        else {
            render(view: "create", model: [documentosInstance: documentosInstance])
        }
    }

        def saveProfe = {
        println params
        def documentosInstance = new Documentos(params)
        def f= request.getFile('myFile')
              if (!f.empty) {
                  def dir = Asignaturas.get(params.asignatura.id).idasignatura
                  f.transferTo(new File('Asignaturas/'+ dir + '/myfile'))
             }
             
        if (documentosInstance.enlace ==  "")
                 documentosInstance.enlace = "http://localhost:8080/educativo/" + params.asignatura.id 
                 
        if (documentosInstance.save(flush: true)) {
            if (params.msg){

               def avisosInstance = new Avisos(
               	 enviadopor: session.user ,
	  	 asunto:"Creado nuevo documento",
		 texto:params.descripcion + " <br> <a href='" + params.enlace + "'> Enlace al documento </a>",
		 fechaenv: new Date(),
	         asignatura: Asignaturas.get(params.asignatura.id),
		 fecharealiz: new Date(),
		 tipo:'documento',
    		 ambito:'privado', 
               )
                 (avisosInstance.save(flush:true))
           
            }     
            
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'documentos.label', default: 'Documentos'), documentosInstance.id])}"
            redirect(action: "showProfe", id: documentosInstance.id)
           
        }
        else {
            render(view: "createProfe", model: [documentosInstance: documentosInstance])
        }
    }

    def show = {
        def documentosInstance = Documentos.get(params.id)
        if (!documentosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'documentos.label', default: 'Documentos'), params.id])}"
            redirect(action: "listProfe")
        }
        else {
            [documentosInstance: documentosInstance]
        }
    }
    def showProfe = {
        def documentosInstance = Documentos.get(params.id)
        if (!documentosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'documentos.label', default: 'Documentos'), params.id])}"
            redirect(action: "listProfe")
        }
        else {
            [documentosInstance: documentosInstance]
        }
    }
    def edit = {
        def documentosInstance = Documentos.get(params.id)
        if (!documentosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'documentos.label', default: 'Documentos'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [documentosInstance: documentosInstance]
        }
    }
    def editProfe = {
        def documentosInstance = Documentos.get(params.id)
        if (!documentosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'documentos.label', default: 'Documentos'), params.id])}"
            redirect(action: "listProfe")
        }
        else {
            return [documentosInstance: documentosInstance]
        }
    }
    def update = {
        def documentosInstance = Documentos.get(params.id)
        if (documentosInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (documentosInstance.version > version) {
                    
                    documentosInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'documentos.label', default: 'Documentos')] as Object[], "Another user has updated this Documentos while you were editing")
                    render(view: "edit", model: [documentosInstance: documentosInstance])
                    return
                }
            }
            documentosInstance.properties = params
            if (!documentosInstance.hasErrors() && documentosInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'documentos.label', default: 'Documentos'), documentosInstance.id])}"
                redirect(action: "show", id: documentosInstance.id)
            }
            else {
                render(view: "edit", model: [documentosInstance: documentosInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'documentos.label', default: 'Documentos'), params.id])}"
            redirect(action: "list")
        }
    }
    def updateProfe = {
        def documentosInstance = Documentos.get(params.id)
        if (documentosInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (documentosInstance.version > version) {
                    
                    documentosInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'documentos.label', default: 'Documentos')] as Object[], "Another user has updated this Documentos while you were editing")
                    render(view: "editProfe", model: [documentosInstance: documentosInstance])
                    return
                }
            }
            documentosInstance.properties = params
            if (!documentosInstance.hasErrors() && documentosInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'documentos.label', default: 'Documentos'), documentosInstance.id])}"
                redirect(action: "showProfe", id: documentosInstance.id)
            }
            else {
                render(view: "editProfe", model: [documentosInstance: documentosInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'documentos.label', default: 'Documentos'), params.id])}"
            redirect(action: "listProfe")
        }
    }

    def delete = {
        def documentosInstance = Documentos.get(params.id)
        if (documentosInstance) {
            try {
                documentosInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'documentos.label', default: 'Documentos'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'documentos.label', default: 'Documentos'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'documentos.label', default: 'Documentos'), params.id])}"
            redirect(action: "list")
        }
    }
    def deleteProfe = {
        def documentosInstance = Documentos.get(params.id)
        if (documentosInstance) {
            try {
                documentosInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'documentos.label', default: 'Documentos'), params.id])}"
                redirect(action: "listProfe")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'documentos.label', default: 'Documentos'), params.id])}"
                redirect(action: "showProfe", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'documentos.label', default: 'Documentos'), params.id])}"
            redirect(action: "listProfe")
        }
    }

}
